﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.EntityClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FaWorks.WebSite.Controllers.Model
{
    public class DisposeTypeController : Controller
    {
        #region CRUD
        [HttpGet, ActionName("CURD")]
        public ActionResult HttpGet()
        {
            return this.Read();
        }
        [HttpPost, ActionName("CURD")]
        public ActionResult HttpPost(Models.Knowledge.DisposeType disposeType)
        {
            return this.Create(disposeType);
        }
        [HttpPut, ActionName("CURD")]
        public ActionResult HttpPut(Int32 id, Models.Knowledge.DisposeType disposeType)
        {
            return this.Update(id, disposeType);
        }
        [HttpDelete, ActionName("CURD")]
        public ActionResult HttpDelete(Int32 id)
        {
            return this.Delete(id);
        }
        #endregion

        public ActionResult Read()
        {
            return Json(KlDB.DisposeTypes, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create(Models.Knowledge.DisposeType disposeType)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                KlDB.DisposeTypes.Add(disposeType);
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Create(disposeType);
            }
            catch (Exception ex) {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            ar.Data = disposeType.id;
            return Json(ar);
        }

        public ActionResult Update(Int32 id, Models.Knowledge.DisposeType disposeType)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {

                var curDisposeType = KlDB.DisposeTypes.Find(id);
                curDisposeType.Name = disposeType.Name;
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Update(curDisposeType);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        public ActionResult Delete(Int32 id)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                var curUser = KlDB.DisposeTypes.Find(id);
                KlDB.DisposeTypes.Remove(curUser);
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Delete(curUser);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        private KlDataBase KlDB = new KlDataBase();
    }
}
